import torch
from torch import nn
from d2l import torch as d2l

# def init_weights(m):
#     if type(m)==nn.Linear:
#         nn.init.normal_(m.weight,std=0.01)  # 将weight定为均值为0，方差为0.01
#
#
# batch_size = 256
# train_iter,test_iter = d2l.load_data_fashion_mnist(batch_size)
#
# net = nn.Sequential(nn.Flatten(),nn.Linear(784,10))
# net.apply(init_weights)
#
# loss = nn.CrossEntropyLoss()
# trainer = torch.optim.SGD(net.parameters(), lr=0.1)
# num_epochs = 50
# d2l.train_ch3(net,train_iter,test_iter,loss,num_epochs,trainer)
# d2l.plt.show()

# 多层感知机
# batch_size = 256
# train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
#
# num_intputs,num_outputs,num_hiddens = 784,10,256
# W1 = nn.Parameter(torch.randn(num_intputs,num_hiddens),requires_grad=True)
# b1 = nn.Parameter(torch.zeros(num_hiddens),requires_grad=True)
# W2 = nn.Parameter(torch.randn(num_hiddens,num_outputs),requires_grad=True)
# b2 = nn.Parameter(torch.zeros(num_outputs),requires_grad=True)
# params = [W1,b1,W2,b2]
#
# def relu(X):
#     a = torch.zeros_like(X)
#     return torch.max(a,X)
#
# def net(X):
#     X = X.reshape((-1,784))
#     H = relu(torch.matmul(X,W1)+b1)
#     return torch.matmul(H,W2)+b2
#
# loss = nn.CrossEntropyLoss()
#
# num_epochs, lr =30,0.01
# updater = torch.optim.SGD(params,lr=lr)
# d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, updater)
# d2l.plt.show()

# 丢弃法
# def dropout_layer(x, dropout):
#     assert 0<= dropout <= 1
#     if dropout == 1:
#         return torch.zeros_like(x)
#     if dropout == 0:
#         return x
#     mask = (torch.randn(x.shape)>dropout).float()
#     return mask * x / (1.0-dropout)
#
# num_inputs,num_outputs,num_hiddens1,num_hiddens2 = 784,10,256,256
# dropout1 ,dropout2 = 0.2,0.5
# class Net(nn.Module):
#     def __init__(self,num_inputs,num_outputs,num_hiddens1,num_hiddens2,is_training=True):
#         super(Net,self).__init__()
#         self.num_inputs = num_inputs
#         self.training = is_training
#         self.lin1 = nn.Linear(num_inputs,num_hiddens1)
#         self.lin2 = nn.Linear(num_hiddens1,num_hiddens2)
#         self.lin3 = nn.Linear(num_hiddens2,num_outputs)
#         self.relu = nn.ReLU()
#     def forward(self,X):
#         H1 = self.relu(self.lin1(X.reshape(-1,self.num_inputs)))
#         if self.training == True:
#             H1 = dropout_layer(H1,dropout1)
#         H2 = self.relu(self.lin2(H1))
#         if self.training == True:
#             H2 = dropout_layer(H2,dropout2)
#         out = self.lin3(H2)
#         return out
#
# net = Net(num_inputs,num_outputs,num_hiddens1,num_hiddens2)
#
# num_epochs , lr ,batch_size = 10,0.5,256
# loss = nn.CrossEntropyLoss()
# train_iter , test_iter = d2l.load_data_fashion_mnist(batch_size)
# trainer = torch.optim.SGD(net.parameters(),lr)
# d2l.train_ch3(net,train_iter,test_iter,loss,num_epochs,trainer)
# d2l.plt.show()

a = torch.randn((5,5))
print(a)
print(a>0.5)